﻿using App.IRepository;
using App.Model;
using App.Model.DTO.Article;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace App.Repository
{
    public class ArticleRepository : BaseRepository<Article>, IArticleRepository
    {
        public async override Task<Article> FindAsync(string id)
        {
            return await base.Context.Queryable<Article>()
               .Includes(a => a.Classification)
               .Includes(a => a.UserInfo)
               .InSingleAsync(id);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<bool> BatchDeleteAsync(List<string> ids)
        {
            return await base.Context.Deleteable<Article>()
                .Where(it => ids.Contains(it.Id))
                .ExecuteCommandHasChangeAsync();
        }
        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public async Task<bool> BatchSoftDeleteAsync(List<string> ids)
        {
            return await base.Context.Updateable<Article>()
                .SetColumns(it => new Article { IsDelete = true })
                .Where(it => ids.Contains(it.Id))
                .ExecuteCommandHasChangeAsync();
        }
        /// <summary>
        /// 批量恢复
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public async Task<bool> BatchRecoveryDeleteAsync(List<string> ids)
        {
            return await base.Context.Updateable<Article>()
                .SetColumns(it => new Article { IsDelete = false })
                .Where(it => ids.Contains(it.Id))
                .ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 置顶
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isTop"></param>
        /// <returns></returns>
        public async Task<bool> SetTopAsync(string id, bool isTop)
        {
            var article = await base.FindAsync(id);
            if (article == null) return false;

            article.IsTop = isTop;
            return await base.EditAsync(article);
        }
        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> SoftDeleteAsync(string id)
        {
            var article = await base.FindAsync(id);
            if (article == null) return false;

            article.IsDelete = true;
            return await base.EditAsync(article);
        }
        /// <summary>
        /// 单个恢复
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> RestoreAsync(string id)
        {
            var article = await base.FindAsync(id);
            if (article == null) return false;
            article.IsDelete = false;
            return await base.EditAsync(article);
        }
        public async override Task<List<Article>> QueryAsync()
        {
            return await base.Context.Queryable<Article>()
                .Mapper(c => c.Classification, c => c.ClassificationId, c => c.Classification.Id)
                .Mapper(u => u.UserInfo, u => u.UserId, u => u.UserInfo.Id)
                .OrderByDescending(a => a.IsTop)
                .OrderBy(a => a.CreateAt, OrderByType.Desc)
                .Where(a => !a.IsDelete)
                .ToListAsync();
        }
        public async override Task<List<Article>> QueryAsync(Expression<Func<Article, bool>> func)
        {
            return await base.Context.Queryable<Article>()
                .Where(func)
                 .Mapper(c => c.Classification, c => c.ClassificationId, c => c.Classification.Id)
                 .Mapper(c => c.UserInfo, c => c.UserId, c => c.UserInfo.Id)
                 .OrderByDescending(a => a.IsTop)
                 .OrderBy(a => a.CreateAt, OrderByType.Desc)
                 .Where(a => !a.IsDelete)
                 .ToListAsync();
        }
        public async override Task<List<Article>> QueryAsync(int pageIndex, int pageSize, RefAsync<int> total)
        {
            return await base.Context.Queryable<Article>()
                .Mapper(c => c.UserInfo, c => c.UserId, c => c.UserInfo.Id)
                .Mapper(c => c.Classification, c => c.ClassificationId, c => c.Classification.Id)
                .OrderByDescending(a => a.IsTop)
                .OrderBy(a => a.CreateAt, OrderByType.Desc)
                .Where(a => !a.IsDelete)
                .ToPageListAsync(pageIndex, pageSize, total);

        }

        public async override Task<List<Article>> QueryAsync(Expression<Func<Article, bool>> func, int pageindex, int pageSize, RefAsync<int> total)
        {
            return await base.Context.Queryable<Article>()
                .Where(func)
                .Mapper(c => c.UserInfo, c => c.UserId, c => c.UserInfo.Id)
                .Mapper(c => c.Classification, c => c.ClassificationId, c => c.Classification.Id)
                .OrderByDescending(a => a.IsTop)
                .OrderBy(a => a.CreateAt, OrderByType.Desc)
                .Where(a => !a.IsDelete)
                .ToPageListAsync(pageindex, pageSize, total);
        }

        public ArticleRepository(ISqlSugarClient context) : base(context)
        {


        }
    }

}
